package leetcode.stack_queue;

import java.util.ArrayDeque;

public class Lc61 {


    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) return null;
        ArrayDeque<ListNode> deque = new ArrayDeque<>();
        while (head != null) {
            deque.offerLast(head);
            head = head.next;
        }
        k = k % deque.size();
        ListNode nhead = null;
        if (k == 0) return deque.pollFirst();
        ListNode cur = deque.peekLast();
        while (k-- != 0) {
            nhead = deque.pollLast();
        }
        deque.peekLast().next = null;
        cur.next = deque.peekFirst();
        return nhead;
    }
}
